﻿using System;
using System.ComponentModel;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using ARS.Control.Custom.DataPaging.Attributes;

namespace ARS.Control.Custom.DropDownListSelect.Widget
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:DropDownListCustom runat=server></{0}:DropDownListCustom>")]
    public class DropDownListCustom : DropDownList
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        private DbSelect _DbSelect;
        [Bindable(true), Category("Appearance"), Description("sql相关属性"), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), NotifyParentProperty(true)]
        public DbSelect AttrDbSelect
        {
            get
            {
                if (_DbSelect == null)
                {
                    _DbSelect = new DbSelect();
                }
                return _DbSelect;
            }
            set
            {
                _DbSelect = value;
            }
        }

        #region 添加add的行
        /// <summary>
        /// 插入行
        /// </summary>
        /// <param name="value"></param>
        /// <param name="text"></param>
        public void AddDtRow(string value, string text)
        {
            AddDtRow(value, text, 0);
        }
        /// <summary>
        /// 插入行
        /// </summary>
        /// <param name="value"></param>
        /// <param name="text"></param>
        /// <param name="index"></param>
        public void AddDtRow(string value, string text, int index)
        {
            if (dataTable != null)
            {
                System.Data.DataRow dr = dataTable.NewRow();
                dr[base.DataValueField] = value;
                dr[base.DataTextField] = text;
                dataTable.Rows.InsertAt(dr, index);
            }
        }
        #endregion

        private DataTable dataTable;
        /// <summary>
        /// 根据dbselect参数绑定数据
        /// </summary>
        public void DataBindCustom()
        {
            if (dataTable == null)
            {
               this.InitDataTable();
            }
            base.DataSource = dataTable;
            base.DataBind();
            //base.DataBind();
        }
        /// <summary>
        /// 初始化datable
        /// </summary>
        public void InitDataTable()
        {
            if (dataTable == null)
            {
                ARS.DataBase.Provider.IDataPagingProvider dpProvider = ARS.DataBase.Provider.DataPagingProvider.GetInstance();
                dataTable = dpProvider.GetDataList(this.AttrDbSelect).Tables[0];
            }
        }

        public void SetValueText(ref string value,ref string text)
        {
            base.DataValueField = value;
            base.DataTextField = text;
        }

        public string ShowAddUrl
        {
            get;
            set;
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            
            base.RenderContents(output);
            //output.Write(Text);
        }
        public override void RenderEndTag(HtmlTextWriter writer)
        {
            base.RenderEndTag(writer);
            if (ShowAddUrl != null)
            {
                writer.Write("<a href=\"" + ShowAddUrl + "\">+</a>");
            }
        }

    }
}
